import pandas as pd
from sklearn import preprocessing
#采样数
N=120-1
#采样时间
T=1
#导入数据
df=pd.read_excel('F:\代码\代码\python\\test.xlsx')
x=df[df.columns[4]].to_numpy()
y=df[df.columns[5]].to_numpy()
x=preprocessing.scale(x,axis=0, with_mean=True, with_std=True, copy=True)
y=preprocessing.scale(y,axis=0, with_mean=True, with_std=True, copy=True)
def CCF(x,y):
    x_=gamma(x[:120])
    y_=gamma(y[:120])
    ave=[]
    for lamda in range (0,N//4):
        z=x_[0:N-lamda]*y_[lamda:N]
        ave.append(z.sum()/(N-lamda))
    if max(ave)+min(ave)>0:
        return [ave.index(max(ave)),max(ave)]
    else:
        return [ave.index(min(ave)),min(ave)]

def gamma(x):
    y=(x[1:]-x[:-1])/T
    return y

CCF(x,y)




'''
x_=gamma(x)
y_=gamma(y)
ave=[]
for lamda in range (0,10):
    z=x_[0:N-lamda]*y_[lamda:N]
    ave.append(z.sum()/(N-lamda))
if max(ave)+min(ave)>0:
    print('max:',ave.index(max(ave)))
else:
    print('min:',ave.index(min(ave)))


ave=[]
lamda=0
z=x_[0:N-lamda]*y_[lamda:N]
ave.append((z.sum())/(N-lamda))
'''